import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';
import 'package:flutter_jpf_core/widgets/jpfbutton.dart';

class SearchBar extends StatefulWidget
{

  static String leftImagePath = "";
  String placeholder;
  TextEditingController etConteoller;
  ValueChanged<String> onValChanged;
  ValueChanged<String> onSearch;
  Widget rightBtn;
  Color inputBgColor;
  EdgeInsets padding;

  SearchBar(this.placeholder,this.etConteoller,{this.onValChanged,this.onSearch,this.inputBgColor ,this.rightBtn,this.padding}){
    if(this.inputBgColor == null){
      this.inputBgColor = ColorsUtil.view_background;
    }
  }

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SearchBarState();
  }

}

class SearchBarState extends State<SearchBar>
{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(child:Row(
      children: [
        Expanded(child: Container(child: TextField(
          controller: widget.etConteoller,
          decoration: InputDecoration(
              border: InputBorder.none,
              prefixIcon: Container(child: Image.asset(SearchBar.leftImagePath),
                margin: EdgeInsets.only(right: 5),),prefixIconConstraints: BoxConstraints(maxWidth: 20,maxHeight: 20),
              hintText: widget.placeholder,hintStyle: TextStyle(fontSize: 14)),
          onChanged: widget.onValChanged??(val){},),decoration: BoxDecoration(
          color: widget.inputBgColor,
          borderRadius: BorderRadius.all(Radius.circular(5))
        ),
        alignment: Alignment(-1.0, 0.0),
        margin: EdgeInsets.only(right: 5,),padding: EdgeInsets.only(left: 5,),) ,flex: 1,),
        GestureDetector(child: widget.rightBtn?? JPFButton("搜索",(){
          if(widget.onSearch != null){
            widget.onSearch(widget.etConteoller.text);
          }
        }??(val){},textColor: ColorsUtil.black_666,bgColors: [Colors.transparent,Colors.transparent],),)
      ],
    )
      ,padding: widget.padding?? EdgeInsets.only(left: 10,right: 10,top: 5,bottom: 5),color: Colors.white,);
  }

}